import config from '../../config';
import request from '../../utils/request';
Page({
    data: {
        isModalVisible: false,
        modalTitle: '',
        modalContent: '',
        baseUrl: config.BASE_URL,
        config: {},
        statusBarHeight: wx.getSystemInfoSync().statusBarHeight,
        navBarHeight: wx.getSystemInfoSync().platform === 'ios' ? 44 : 48,
        userInfo: {},
        menuItems: [],
        arrowIcon: '/assets/images/arrow_right_icon.png' // 列表项右侧箭头图标
    },
    loginPage() {
        if (!wx.getStorageSync('token')) {
            wx.navigateTo({
                url: '/pages/login/login',
            })
        }
    },
    onLoad: function (options) {

    },
    onShow: function () {
        console.log('onShow');
        if (typeof this.getTabBar === 'function' && this.getTabBar()) {
            this.getTabBar().setData({
                selected: 1 // 这里的数字1对应tabBar的list中个人中心项的索引（从0开始）
            });
        }
        if (wx.getStorageSync('token')) {
            this.getLoginInfo();
        } else {
            let menuItems = [{
                icon: '/assets/images/uc_profile_icon.png',
                title: '个人资料',
                page: '/pages/profile/profile'
            }, {
                icon: '/assets/images/uc_team_icon.png',
                title: '我的团队',
                page: '/pages/my-team/my-team'
            }, {
                icon: '/assets/images/uc_tasks_icon.png',
                title: '我的任务',
                page: '/pages/my-tasks/my-tasks'
            }, {
                icon: '/assets/images/uc_withdrawal_icon.png',
                title: '提现申请',
                page: '/pages/withdrawal-apply/withdrawal-apply'
            }, {
                icon: '/assets/images/uc_service_icon.png',
                title: '在线客服',
                type: 'contact'
            }, {
                icon: '/assets/images/uc_invite_icon.png',
                title: '邀请好友',
                type: 'share'
            }]
            this.setData({
                menuItems: menuItems
            })
        }
        this.getConfig();
    },
    getConfig: function () {
        if (!wx.getStorageSync('token')) {
            return;
        }
        request({
            url: 'api/login/getConfig',
        }).then(res => {
            if (res.data) {
                wx.setStorageSync('config', res.data);
                this.setData({
                    config: res.data
                });
                let menuItems = [{
                    icon: '/assets/images/uc_profile_icon.png',
                    title: '个人资料',
                    page: '/pages/profile/profile'
                }]
                let config = res.data;
                if (config.showMyTeam) {
                    menuItems.push({
                        icon: '/assets/images/uc_team_icon.png',
                        title: '我的团队',
                        page: '/pages/my-team/my-team'
                    })
                }
                if (config.showMyTasks) {
                    menuItems.push({
                        icon: '/assets/images/uc_tasks_icon.png',
                        title: '我的任务',
                        page: '/pages/my-tasks/my-tasks'
                    })
                }
                if (config.showWithdrawalApplication) {
                    menuItems.push({
                        icon: '/assets/images/uc_withdrawal_icon.png',
                        title: '提现申请',
                        page: '/pages/withdrawal-apply/withdrawal-apply'
                    })
                }
                menuItems.push({
                    icon: '/assets/images/uc_service_icon.png',
                    title: '在线客服',
                    type: 'contact'
                })
                if (config.showInviteFriends) {
                    menuItems.push({
                        icon: '/assets/images/uc_invite_icon.png',
                        title: '邀请好友',
                        type: 'share'
                    })
                }
                this.setData({
                    menuItems: menuItems
                })
            }
        });
    },
    getLoginInfo: function () {
        if (!wx.getStorageSync('token')) {
            return;
        }
        request({
            url: 'api/login/getLoginInfo',
            method: 'POST',
        }).then(res => {
            if (res.data) {
                wx.setStorageSync('userInfo', res.data);
                this.setData({
                    userInfo: res.data
                });
            }
        });
    },
    // 处理菜单项点击
    handleMenuItemTap: function (e) {
        const item = e.currentTarget.dataset.item;
        if (!wx.getStorageSync('token')) {
            this.setData({
                isModalVisible: true,
                modalTitle: '提示',
                modalContent: '请先登录'
            });
            return;
        }
        if (item.page) {
            if (item.title === '提现申请' && (!this.data.userInfo.paymentCode || !this.data.userInfo.wxAccount)) {
                this.setData({
                    isModalVisible: true,
                    modalTitle: '提示',
                    modalContent: '请先绑定微信账户以及上传收款码，才能完成提现操作。'
                });
                return;
            }
            wx.navigateTo({
                url: item.page
            });
        }
    },
    // 退出登录
    handleLogout: function () {
        wx.showModal({
            title: '提示',
            content: '确定要退出登录吗？',
            success: (res) => {
                if (res.confirm) {
                    console.log('用户点击确定退出');
                    // 在这里执行实际的退出登录逻辑，例如清除本地存储的用户信息、token等
                    wx.removeStorageSync('userInfo');
                    wx.removeStorageSync('token');
                    wx.removeStorageSync('config');
                    // 然后跳转到登录页
                    wx.reLaunch({ // reLaunch 会关闭所有页面，打开到应用内的某个页面
                        url: '/pages/index/index'
                    });
                }
            }
        });
    },
    // 允许页面分享
    onShareAppMessage: function () {
        return {
            title: '邀请你加入喵集令，一起完成任务吧！',
            path: '/pages/index/index?referenceId=' + (this.data.userInfo.id || ''), // 可以带上邀请者ID
            imageUrl: '/assets/images/share_1.png' // 自定义分享图片，你需要准备这张图片
        };
    }
});